/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JSONParser;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
/**
 * Met deze klasse kun je de data binnenhalen van de server en scheiden in drie soorten: alarmen, analoge sensoren en binaire sensoren.
 * Zolang een socket alive is zullen sensorwaardes in de ArrayListen worden geladen. De alarmen, binaire en analoge sensoren zitten allemaal in een aparte 
 * ArrayList zodat je makkelijk er doorheen kunt lopen en voor enkle waarde een sensor kunt maken.
 * @author David de Hoop
 */
public class TCPSocket {
    String ipadress = "145.92.78.217";
    int portnumber = 81;
    String jsonString;
    
    ArrayList<JSONObject> jsonArrayAlarm = new ArrayList<>();
    ArrayList<JSONObject> jsonArrayAnalogSensors = new ArrayList<>();
    ArrayList<JSONObject> jsonArrayBinarySensors = new ArrayList<>();
         
    public void TCPSocket(String ip, int port) {
        ipadress = ip;
        portnumber = port;
    }
    
    public void Socket() throws IOException{
        Socket TCPSocket = new Socket(ipadress, portnumber);
        TCPSocket.setKeepAlive(true);
        
        DataInputStream inFromServer = new DataInputStream(TCPSocket.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(inFromServer, "UTF-8"));
        
        while(TCPSocket.getKeepAlive()) {
            jsonString = br.readLine();
                        
            JSONObject jsonStringObject = (JSONObject) JSONSerializer.toJSON(jsonString);                      
            
            JSONObject jsonAlarms = (JSONObject) jsonStringObject.get("alarm");
            jsonArrayAlarm.add(jsonAlarms);      

			JSONArray jsonAnalogSensors =  (JSONArray) jsonStringObject.get("analogSensors");
            for (int i = 0; i < jsonAnalogSensors.size(); i++) {
			   JSONObject jsonObject = jsonAnalogSensors.getJSONObject(i);
			   jsonArrayAnalogSensors.add(jsonObject);
			}

			JSONArray jsonBinarySensors =  (JSONArray) jsonStringObject.get("binarySensors");
            for (int i = 0; i < jsonBinarySensors.size(); i++) {
			   JSONObject jsonObject = jsonBinarySensors.getJSONObject(i);
			   jsonArrayBinarySensors.add(jsonObject);
			}          
                            
        }  
    }          
}
      
    





    
   